/** m2settings is a GUI to configure Maven 2 settings.xml 
 * 
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 * 
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version. 
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
/*
 * MainWindow.java
 *
 * Created on March 16, 2007, 10:52 PM
 */

package info.decamps.m2settings.swing;

import info.decamps.m2settings.Constant;
import info.decamps.m2settings.SettingsXMLFileBinder;
import info.decamps.m2settings.documentation.DocumentationProvider;
import info.decamps.m2settings.model.Mirror;
import info.decamps.m2settings.model.MirrorTableModel;
import info.decamps.m2settings.model.ProfilesTableModel;
import info.decamps.m2settings.model.ProxyTableModel;
import info.decamps.m2settings.model.Server;
import info.decamps.m2settings.model.ServerTableModel;
import info.decamps.m2settings.model.Servers;
import info.decamps.m2settings.model.Settings;
import info.decamps.m2settings.swing.JFrameMirror;
import info.decamps.m2settings.swing.JFrameServer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.ResourceBundle;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;

/**
 * Main window of the m2settings application.
 * http://code.google.com/p/m2settings/
 * @author Regis Decamps (decamps@users.sf.net)
 */
public class MainWindow extends javax.swing.JFrame {
    private Logger log = Logger.getLogger(MainWindow.class);
    
    private SettingsXMLFileBinder xmlbind;
    private Settings settings; // a pointer to xmlbind.getSettings();
    private DocumentationProvider documentation=DocumentationProvider.getInstance(); //a pointer to the singleton
    
    /** Creates new form MainWindow */
    public MainWindow() {
        log.debug("welcome $Id$");
        log.info(Constant.getStartupNotice());   
        reloadSettings();
        initComponents();
        
    }
    
    private void reloadSettings(final File settingsFile) throws FileNotFoundException, ValidationException, MarshalException {
        xmlbind=new SettingsXMLFileBinder(settingsFile);
        settings=xmlbind.getSettings();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTabbedPane = new javax.swing.JTabbedPane();
        jPanelGeneral = new javax.swing.JPanel();
        jLabelLocalRepo = new javax.swing.JLabel();
        jTextFieldLocalRepo = new javax.swing.JTextField();
        jButtonLocalRepoBrowser = new javax.swing.JButton();
        jCheckBoxInteractiveMode = new javax.swing.JCheckBox();
        jCheckBoxOffline = new javax.swing.JCheckBox();
        JPanelProfiles = new javax.swing.JPanel();
        jScrollPaneProfiles = new javax.swing.JScrollPane();
        jTableProfiles = new javax.swing.JTable();
        jButtonRemoveProfile = new javax.swing.JButton();
        jButtonAddProfile = new javax.swing.JButton();
        jButtonEditProfile = new javax.swing.JButton();
        jPanelMirrors = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableMirrors = new javax.swing.JTable();
        jButtonAddMirror = new javax.swing.JButton();
        jButtonRemoveMirror = new javax.swing.JButton();
        jButtonEditMirror = new javax.swing.JButton();
        jPanelProxies = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTableProxies = new javax.swing.JTable();
        jButtonAddProxy = new javax.swing.JButton();
        jButtonRemoveProxy = new javax.swing.JButton();
        jButtonEditProxy = new javax.swing.JButton();
        jPanelServers = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableServers = new javax.swing.JTable();
        jButtonAddServer = new javax.swing.JButton();
        jButtonRemoveServer = new javax.swing.JButton();
        jButtonEditServer = new javax.swing.JButton();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemLoad = new javax.swing.JMenuItem();
        jMenuItemQuit = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuHelp = new javax.swing.JMenu();
        jMenuItemHelpLicense = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(java.util.ResourceBundle.getBundle("messages").getString("program_name"));
        jLabelLocalRepo.setText("Local Repository");

        jTextFieldLocalRepo.setText(settings.getLocalRepository());
        jTextFieldLocalRepo.setToolTipText(documentation.getDescription("/Settings/localRepository"));

        jButtonLocalRepoBrowser.setText("...");
        jButtonLocalRepoBrowser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLocalRepoBrowserActionPerformed(evt);
            }
        });

        jCheckBoxInteractiveMode.setSelected(settings.getInteractiveMode());
        jCheckBoxInteractiveMode.setText("interactive mode");
        jCheckBoxInteractiveMode.setToolTipText(documentation.getDescription("/Settings/interactiveMode"));
        jCheckBoxInteractiveMode.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBoxInteractiveMode.setMargin(new java.awt.Insets(0, 0, 0, 0));

        jCheckBoxOffline.setSelected(settings.getOffline());
        jCheckBoxOffline.setText("offline");
        jCheckBoxOffline.setToolTipText(documentation.getDescription("/Settings/offline"));
        jCheckBoxOffline.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBoxOffline.setMargin(new java.awt.Insets(0, 0, 0, 0));

        org.jdesktop.layout.GroupLayout jPanelGeneralLayout = new org.jdesktop.layout.GroupLayout(jPanelGeneral);
        jPanelGeneral.setLayout(jPanelGeneralLayout);
        jPanelGeneralLayout.setHorizontalGroup(
            jPanelGeneralLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelGeneralLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelGeneralLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanelGeneralLayout.createSequentialGroup()
                        .add(jLabelLocalRepo)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldLocalRepo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonLocalRepoBrowser))
                    .add(jCheckBoxInteractiveMode)
                    .add(jCheckBoxOffline))
                .addContainerGap())
        );
        jPanelGeneralLayout.setVerticalGroup(
            jPanelGeneralLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelGeneralLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelGeneralLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelLocalRepo)
                    .add(jButtonLocalRepoBrowser)
                    .add(jTextFieldLocalRepo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jCheckBoxInteractiveMode)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jCheckBoxOffline)
                .addContainerGap(98, Short.MAX_VALUE))
        );
        jTabbedPane.addTab("General", jPanelGeneral);

        JPanelProfiles.setToolTipText(documentation.getDescription("/Settings/profiles"));
        jScrollPaneProfiles.setMinimumSize(new java.awt.Dimension(10, 5));
        jTableProfiles.setModel(new info.decamps.m2settings.model.ProfilesTableModel(settings.getProfiles()));
        jScrollPaneProfiles.setViewportView(jTableProfiles);

        jButtonRemoveProfile.setText("Remove");
        jButtonRemoveProfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveProfileActionPerformed(evt);
            }
        });

        jButtonAddProfile.setText("Add");
        jButtonAddProfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddProfileActionPerformed(evt);
            }
        });

        jButtonEditProfile.setText("Edit");
        jButtonEditProfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditProfileActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout JPanelProfilesLayout = new org.jdesktop.layout.GroupLayout(JPanelProfiles);
        JPanelProfiles.setLayout(JPanelProfilesLayout);
        JPanelProfilesLayout.setHorizontalGroup(
            JPanelProfilesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPaneProfiles, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 475, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(JPanelProfilesLayout.createSequentialGroup()
                .addContainerGap()
                .add(jButtonAddProfile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonEditProfile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 173, Short.MAX_VALUE)
                .add(jButtonRemoveProfile)
                .addContainerGap())
        );
        JPanelProfilesLayout.setVerticalGroup(
            JPanelProfilesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, JPanelProfilesLayout.createSequentialGroup()
                .add(jScrollPaneProfiles, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 10, Short.MAX_VALUE)
                .add(JPanelProfilesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonAddProfile)
                    .add(jButtonEditProfile)
                    .add(jButtonRemoveProfile))
                .addContainerGap())
        );
        jTabbedPane.addTab("Profiles", JPanelProfiles);

        jPanelMirrors.setToolTipText(documentation.getDescription("/Settings/mirrors") );
        jScrollPane2.setMinimumSize(new java.awt.Dimension(10, 5));
        jTableMirrors.setModel(new info.decamps.m2settings.model.MirrorTableModel(settings));
        jScrollPane2.setViewportView(jTableMirrors);

        jButtonAddMirror.setText("Add");
        jButtonAddMirror.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddMirrorActionPerformed(evt);
            }
        });

        jButtonRemoveMirror.setText("Remove");
        jButtonRemoveMirror.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveMirrorActionPerformed(evt);
            }
        });

        jButtonEditMirror.setText("Edit");
        jButtonEditMirror.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditMirrorActionPerformed(evt);
            }
        });

        jButtonEditMirror.getAccessibleContext().setAccessibleName("EditMirror");

        org.jdesktop.layout.GroupLayout jPanelMirrorsLayout = new org.jdesktop.layout.GroupLayout(jPanelMirrors);
        jPanelMirrors.setLayout(jPanelMirrorsLayout);
        jPanelMirrorsLayout.setHorizontalGroup(
            jPanelMirrorsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelMirrorsLayout.createSequentialGroup()
                .addContainerGap()
                .add(jButtonAddMirror, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonEditMirror, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 173, Short.MAX_VALUE)
                .add(jButtonRemoveMirror)
                .addContainerGap())
            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
        );
        jPanelMirrorsLayout.setVerticalGroup(
            jPanelMirrorsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelMirrorsLayout.createSequentialGroup()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelMirrorsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonAddMirror)
                    .add(jButtonRemoveMirror)
                    .add(jButtonEditMirror))
                .addContainerGap())
        );
        jTabbedPane.addTab("Mirrors", null, jPanelMirrors, "<User Code>");

        jPanelProxies.setToolTipText(documentation.getDescription("/Settings/proxies"));
        jScrollPane4.setMinimumSize(new java.awt.Dimension(10, 5));
        jTableProxies.setModel(new info.decamps.m2settings.model.ProxyTableModel(settings.getProxies()));
        jScrollPane4.setViewportView(jTableProxies);

        jButtonAddProxy.setText("Add");
        jButtonAddProxy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddProxyActionPerformed(evt);
            }
        });

        jButtonRemoveProxy.setText("Remove");
        jButtonRemoveProxy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveProxyActionPerformed(evt);
            }
        });

        jButtonEditProxy.setText("Edit");
        jButtonEditProxy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditProxyActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelProxiesLayout = new org.jdesktop.layout.GroupLayout(jPanelProxies);
        jPanelProxies.setLayout(jPanelProxiesLayout);
        jPanelProxiesLayout.setHorizontalGroup(
            jPanelProxiesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelProxiesLayout.createSequentialGroup()
                .addContainerGap()
                .add(jButtonAddProxy, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonEditProxy, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 173, Short.MAX_VALUE)
                .add(jButtonRemoveProxy)
                .addContainerGap())
            .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
        );
        jPanelProxiesLayout.setVerticalGroup(
            jPanelProxiesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelProxiesLayout.createSequentialGroup()
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelProxiesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonAddProxy)
                    .add(jButtonRemoveProxy)
                    .add(jButtonEditProxy))
                .addContainerGap())
        );
        jTabbedPane.addTab("Proxies", null, jPanelProxies, "");

        jPanelServers.setToolTipText(documentation.getDescription("/Settings/servers") );
        jScrollPane3.setMinimumSize(new java.awt.Dimension(10, 5));
        jTableServers.setModel(new info.decamps.m2settings.model.ServerTableModel(settings));
        jScrollPane3.setViewportView(jTableServers);

        jButtonAddServer.setText("Add");
        jButtonAddServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddServerActionPerformed(evt);
            }
        });

        jButtonRemoveServer.setText("Remove");
        jButtonRemoveServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveServerActionPerformed(evt);
            }
        });

        jButtonEditServer.setText("Edit");
        jButtonEditServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditServerActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelServersLayout = new org.jdesktop.layout.GroupLayout(jPanelServers);
        jPanelServers.setLayout(jPanelServersLayout);
        jPanelServersLayout.setHorizontalGroup(
            jPanelServersLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelServersLayout.createSequentialGroup()
                .addContainerGap()
                .add(jButtonAddServer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonEditServer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 173, Short.MAX_VALUE)
                .add(jButtonRemoveServer)
                .addContainerGap())
            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
        );
        jPanelServersLayout.setVerticalGroup(
            jPanelServersLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelServersLayout.createSequentialGroup()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelServersLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonAddServer)
                    .add(jButtonRemoveServer)
                    .add(jButtonEditServer))
                .addContainerGap())
        );
        jTabbedPane.addTab("Servers", null, jPanelServers, "Configuration of server-specific settings, mainly authentication method. This allows configuration of authentication on a per-server basis.");

        jMenuFile.setText("File");
        jMenuItemLoad.setText("Reload settings.xml");
        jMenuItemLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemLoadActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemLoad);

        jMenuItemQuit.setText("Cancel and quit");
        jMenuItemQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemQuitActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemQuit);

        jMenuItemExit.setText(java.util.ResourceBundle.getBundle("messages").getString("Save_and_exit"));
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemExit);

        jMenuBar.add(jMenuFile);

        jMenuHelp.setText("Help");
        jMenuItemHelpLicense.setText("License");
        jMenuItemHelpLicense.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemHelpLicenseActionPerformed(evt);
            }
        });

        jMenuHelp.add(jMenuItemHelpLicense);

        jMenuBar.add(jMenuHelp);

        setJMenuBar(jMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemHelpLicenseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemHelpLicenseActionPerformed
        new JFrameLicense().setVisible(true);
    }//GEN-LAST:event_jMenuItemHelpLicenseActionPerformed

    private void jButtonRemoveMirrorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveMirrorActionPerformed
        MirrorTableModel model=(MirrorTableModel)jTableMirrors.getModel();
        model.removeRows(jTableMirrors.getSelectedRows());
    }//GEN-LAST:event_jButtonRemoveMirrorActionPerformed

    private void jButtonRemoveProxyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveProxyActionPerformed
        ProxyTableModel model=(ProxyTableModel)jTableProxies.getModel();
        model.removeRows(jTableProxies.getSelectedRows());
    }//GEN-LAST:event_jButtonRemoveProxyActionPerformed
    
    private void jButtonRemoveProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveProfileActionPerformed
        ProfilesTableModel model=(ProfilesTableModel)jTableProfiles.getModel();
        model.removeRows(jTableProfiles.getSelectedRows());
    }//GEN-LAST:event_jButtonRemoveProfileActionPerformed
    
    private void jButtonEditProxyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditProxyActionPerformed
        int index=jTableProxies.getSelectedRow();
        log.debug("please edit proxy #"+index);
        if (index>=0){
            JFrameProxy proxyWin=new JFrameProxy((ProxyTableModel)jTableProxies.getModel(),index);
            proxyWin.setVisible(true);
        }
    }//GEN-LAST:event_jButtonEditProxyActionPerformed
    
    private void jButtonAddProxyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddProxyActionPerformed
        JFrameProxy proxyWin=new JFrameProxy((ProxyTableModel)jTableProxies.getModel());
        log.debug("please configure new proxy");
        proxyWin.setVisible(true);
    }//GEN-LAST:event_jButtonAddProxyActionPerformed
    
    private void jButtonEditProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditProfileActionPerformed
        int index=jTableProfiles.getSelectedRow();
        log.debug("please configure profile #"+index);
        if (index>=0) {
            JFrameProfile profileWin=new JFrameProfile((ProfilesTableModel)jTableProfiles.getModel(),index);
            profileWin.setVisible(true);
        }
    }//GEN-LAST:event_jButtonEditProfileActionPerformed
    
    private void jButtonAddProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddProfileActionPerformed
        JFrameProfile profileWin=new JFrameProfile((ProfilesTableModel)jTableProfiles.getModel());
        log.debug("please configure new profile");
        profileWin.setVisible(true);
    }//GEN-LAST:event_jButtonAddProfileActionPerformed
    
    private void jButtonRemoveServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveServerActionPerformed
        ServerTableModel model=(ServerTableModel)jTableServers.getModel();
        model.removeRows(jTableServers.getSelectedRows());
    }//GEN-LAST:event_jButtonRemoveServerActionPerformed
    
    private void jButtonEditServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditServerActionPerformed
        int index=jTableServers.getSelectedRow();
        if (index>=0){
            JFrameServer serverEditWin= new JFrameServer((ServerTableModel)jTableServers.getModel(),index);
            log.debug("please edit server");
            serverEditWin.setVisible(true);
        }
    }//GEN-LAST:event_jButtonEditServerActionPerformed
    
    private void jButtonEditMirrorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditMirrorActionPerformed
        JFrameMirror mirrorWin=new JFrameMirror((MirrorTableModel)jTableMirrors.getModel(),jTableMirrors.getSelectedRow());
        log.debug("please edit mirror");
        mirrorWin.setVisible(true);
    }//GEN-LAST:event_jButtonEditMirrorActionPerformed
    
    private void jButtonAddServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddServerActionPerformed
        JFrameServer serverWin=new JFrameServer((ServerTableModel)jTableServers.getModel());
        log.debug("Please add a new server");
        serverWin.setVisible(true);
    }//GEN-LAST:event_jButtonAddServerActionPerformed
    
private void jButtonAddMirrorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddMirrorActionPerformed
    JFrameMirror mirrorWin=new JFrameMirror((MirrorTableModel)jTableMirrors.getModel());
    log.debug("please add a new mirror");
    mirrorWin.setVisible(true);
}//GEN-LAST:event_jButtonAddMirrorActionPerformed

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        log.debug("Save and exit");
        saveSettings();
        System.exit(0);
    }//GEN-LAST:event_jMenuItemExitActionPerformed
    
    private void jMenuItemQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemQuitActionPerformed
        log.debug("Quit");
        //TODO confirm dialog
        System.exit(0);
    }//GEN-LAST:event_jMenuItemQuitActionPerformed
    
    private void jButtonLocalRepoBrowserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLocalRepoBrowserActionPerformed
        JFileChooser jFileChooser1= new JFileChooser();
        jFileChooser1.setFileHidingEnabled(false);
        jFileChooser1.setDialogTitle("Select Maven Local repository");
        
        String currentRepo=settings.getLocalRepository();
        if (currentRepo!=null) {
            File currentM2Repo=new File(settings.getLocalRepository());
            log.debug("current is"+currentRepo);
            if (currentM2Repo!=null){
                jFileChooser1.ensureFileIsVisible(currentM2Repo);
                jFileChooser1.setSelectedFile(currentM2Repo);
                log.debug("selected");
            }
        }
        
        jFileChooser1.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        int returnVal = jFileChooser1.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File m2repo = jFileChooser1.getSelectedFile();
            log.debug("Opening: " + m2repo.getName() + "." );
            settings.setLocalRepository(m2repo.getPath());
            jTextFieldLocalRepo.setText(m2repo.getName());
        } else {
            log.debug("Open command cancelled by user." );
        }
    }//GEN-LAST:event_jButtonLocalRepoBrowserActionPerformed
    
    private void jMenuItemLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadActionPerformed
        reloadSettings();
    }//GEN-LAST:event_jMenuItemLoadActionPerformed
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MainWindow thisClass = new MainWindow();
                thisClass.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel JPanelProfiles;
    private javax.swing.JButton jButtonAddMirror;
    private javax.swing.JButton jButtonAddProfile;
    private javax.swing.JButton jButtonAddProxy;
    private javax.swing.JButton jButtonAddServer;
    private javax.swing.JButton jButtonEditMirror;
    private javax.swing.JButton jButtonEditProfile;
    private javax.swing.JButton jButtonEditProxy;
    private javax.swing.JButton jButtonEditServer;
    private javax.swing.JButton jButtonLocalRepoBrowser;
    private javax.swing.JButton jButtonRemoveMirror;
    private javax.swing.JButton jButtonRemoveProfile;
    private javax.swing.JButton jButtonRemoveProxy;
    private javax.swing.JButton jButtonRemoveServer;
    private javax.swing.JCheckBox jCheckBoxInteractiveMode;
    private javax.swing.JCheckBox jCheckBoxOffline;
    private javax.swing.JLabel jLabelLocalRepo;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemHelpLicense;
    private javax.swing.JMenuItem jMenuItemLoad;
    private javax.swing.JMenuItem jMenuItemQuit;
    private javax.swing.JPanel jPanelGeneral;
    private javax.swing.JPanel jPanelMirrors;
    private javax.swing.JPanel jPanelProxies;
    private javax.swing.JPanel jPanelServers;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPaneProfiles;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JTable jTableMirrors;
    private javax.swing.JTable jTableProfiles;
    private javax.swing.JTable jTableProxies;
    private javax.swing.JTable jTableServers;
    private javax.swing.JTextField jTextFieldLocalRepo;
    // End of variables declaration//GEN-END:variables
    
    /**
     * Reload the settings from settings.xml
     * In case of error, displays an JDialog alert, and constructs new settings
     **/
    private void reloadSettings()  {
        log.debug("Load settings.xml");
        try {
            xmlbind=new SettingsXMLFileBinder();
            xmlbind.loadSettings();
            settings=xmlbind.getSettings();
        } catch (FileNotFoundException e) {
            log.warn("settings.xml not found. Settings are initialized empty.");
            // if settings are null, return new.
            settings=xmlbind.getSettings();
        } catch (ValidationException e) {
            alert("Error loading settings","settings.xml corrupted");
        } catch (MarshalException e) {
            alert("Error loading settings","settings.xml XML invalid");
        }
        if (settings==null) {
            settings=new Settings();
            xmlbind.setSettings(settings);
        }
    }
    
    /**
     * Displays a JDialog alert.
     */
    public void alert(String title,String message) {
        JOptionPane jOptionPaneAlert=new JOptionPane();
        JDialog jDialogAlert=new JDialog(this);
        jDialogAlert.setTitle(title);
        jDialogAlert.add(jOptionPaneAlert);
        jOptionPaneAlert.setMessage(message);
        jDialogAlert.setVisible(true);
    }
    
    private void saveSettings() {
        //set new parameters
        settings.setInteractiveMode(jCheckBoxInteractiveMode.isSelected());
        settings.setOffline(jCheckBoxOffline.isSelected());
        if (jTextFieldLocalRepo.getText().length()==0) {
            settings.setLocalRepository(null);
        } else {
            settings.setLocalRepository(jTextFieldLocalRepo.getText());
        }        
        settings.setProfiles(
                ((ProfilesTableModel)jTableProfiles.getModel()).getProfiles() );
        settings.setMirrors(
                ((MirrorTableModel)jTableMirrors.getModel()).getMirrors()  );
        settings.setProxies(
                ((ProxyTableModel)jTableProxies.getModel()).getProxies() );
        settings.setServers(
                ((ServerTableModel)jTableServers.getModel()).getServers()  );
        try {
            xmlbind.saveSettings();
        } catch (MarshalException ex) {
            ex.printStackTrace();
        } catch (ValidationException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            alert("Cannot save settings",ex.getMessage());
        } finally{
            System.exit(1);
        }
    }
}